/*
 * @lc app=leetcode.cn id=1 lang=javascript
 *
 * [1] 两数之和
 */

// @lc code=start
/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number[]}
 */
var twoSum = function (nums, target) {
    return hash(nums, target)
};

// 哈希 O(n)
function hash(nums, target) {
    let map = {}
    for (let i = 0; i < nums.length; i++) {
        let key = target - nums[i]
        if (map[key] == undefined) {
            map[key] = i
        }
        if (map[nums[i]] != undefined && map[key] != undefined) {
            if (map[key] != map[nums[i]]) {
                return [map[key], map[nums[i]]]
            } else if (map[key] != i) {
                return [map[key], i]
            }
        }
    }
}

// @lc code=end


// @after-stub-for-debug-begin
module.exports = twoSum;
// @after-stub-for-debug-end